<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="./script/rem.js"></script>
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
</head>

<body>

    <header class="head" style="padding-top:0.4rem;" id="head" >
        <div class="head-l" tapmode onclick="closeWin()"><i class="fa fa-angle-left"></i></div>
        <h3 id="title">财富之路</h3>
        <div class="head-r"></div>
    </header>
    <div class="head-d" style="padding-top:0.4rem;"></div>

    <div id='list'>

    </div>

</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./script/index.js"></script>
<script type="text/javascript" src="./script/aui-slide.js"></script>
<script type="text/template" id="template">

    <div>{{=it}}</div>

</script>
<script type="text/javascript">
    apiready = function() {

        getData();
    }

    function getData() {
        var token = $api.getStorage('token');
        // 进度提示框
        api.showProgress({
            title: '努力加载中...',
            text: '请稍等...',
            modal: false
        });

        var id = api.pageParam.id;

        var data_url = webAddress + '/api/wap/html?key=money_road';
        api.ajax({
            url: data_url,
            method: 'get',
            data: {
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        }, function(ret, err) {
            if (ret) {
                // 关闭进度提示
                api.hideProgress();

                // alert(JSON.stringify(ret));

                var template = $api.byId('template');
                var dotFn = doT.template(template.innerHTML);
                var html = dotFn(ret.body);
                var list = $api.byId('list');
                $api.html(list, html);

                // $('img').css('width','100%');
                // $('img').css('height','auto');

            } else {
                // alert(JSON.stringify(err.body));
                var template = $api.byId('template');
                var dotFn = doT.template(template.innerHTML);
                var html = dotFn(err.body);
                var list = $api.byId('list');
                $api.html(list, html);

                // $('img').css('width','100%');
                // $('img').css('height','auto');

                // 关闭进度提示
                api.hideProgress();
            }
        });
    }


    // 下拉刷新
    function pullRefresh() {
        api.setRefreshHeaderInfo({
            // loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...'
        }, function(ret, err) {
            getData();
        });
    }


    function shop_order_details() {
        api.openWin({
            name: 'shop_order_details',
            url: './shop_order_details.html',
        });
    }

    function closeWin() {
        api.closeWin({});
    }
    // var slide = new auiSlide({
    //     container: document.getElementById("aui-slide"),
    //     // "width":300,
    //     "height": 260,
    //     "speed": 300,
    //     "pageShow": true,
    //     "autoPlay": 3000, //自动播放
    //     "pageStyle": 'dot',
    //     "loop": true,
    //     'dotPosition': 'center',
    //     currentPage: currentFun
    // })

    function currentFun(index) {
        // console.log(index);
    }


    function add_cart(index) {
      var temp= $(".shop-cart-bg").is(":visible");//是否可见

      if (temp ==false) {
        $(".shop-cart-bg").show();
        $(".goods-box").animate({
          bottom:"1rem"
      });
    }else {
        var count = $('#count').val();
        api.ajax({
            url: webAddress + '/api/cart/add',
            method: 'post',
            headers: {
                'Authorization': token
            },
            data: {
                values: {
                    product_id: index,
                    number: count,
                    category: 1
                },
            }
        }, function(ret, err) {
            if (ret) {
                api.toast({
                    msg: '添加购物车成功!',
                    duration: 2000,
                    location: 'bottom'
                });
                $(".shop-cart-bg").hide();
                api.execScript({
                    name: 'root',
                    frameName: 'shop',
                    script: 'get_cart();'
                });


            } else {
                // alert(JSON.stringify(err));
            }
        });

    }

}

function goods_details_on(index){
      var temp= $(".shop-cart-bg").is(":visible");//是否可见

      if (temp ==false) {
        $(".shop-cart-bg").show();
        $(".goods-box").animate({
          bottom:"1rem"
      });
    }else {
      var count = $('#count').val();

      api.ajax({
          url: webAddress + '/api/cart/add',
          method: 'post',
          headers: {
              'Authorization': token
          },
          data: {
              values: {
                  product_id: index,
                  number: count,
                  category: 2
              },
          }
      }, function(ret, err) {
          if (ret) {
            var ids = ret.data.id;
            api.openWin({
                name: 'shop_order_details',
                url: './shop_order_details.html',
                pageParam: {
                    ids: ids
                }
            });
        } else {
          // alert(JSON.stringify(err));
      }
  });



  }


}


function collection(){
    var id = goods_id;
    var token = $api.getStorage('token');
    api.ajax({
        url: webAddress + '/api/product/' + id + '/collection',
        method: 'get',
        data: {

        },
        headers: {
            Authorization: 'Bearer ' + token,
            Accept: 'application/json',
        },
    },function(ret, err){
        if (ret) {
            // alert( JSON.stringify( ret ) );
            if (ret.status_code == 200) {
                if(ret.data.is_collection == 1){
                    api.toast({
                        msg: '收藏成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    $("#collection_id").attr("class","off");
                }else if(ret.data.is_collection == 0){
                    api.toast({
                        msg: '取消收藏成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    $("#collection_id").attr("class","on");
                }
                api.sendEvent({
                  name: 'new_reload'
              });
            }
        } else {
            api.toast({
                msg: '网络错误',
                duration: 2000,
                location: 'bottom'
            });
        }
    });
}


function change_count(type) {

  var count = $('#count').val();
  if (type == 'jia') {
      count = parseInt(count) + 1;
      $('#count').val(count);

  } else {
    if (count <= 1) {
      return;
  }
  count = parseInt(count) - 1;
  $('#count').val(count);
}

}


    // function goods_details_on(index) {
    //   api.ajax({
    //       url: webAddress + '/api/cart/add',
    //       method: 'post',
    //       headers: {
    //           'Authorization': token
    //       },
    //       data: {
    //           values: {
    //               product_id: index,
    //               number: 1,
    //               category: 1
    //           },
    //       }
    //   }, function(ret, err) {
    //       if (ret) {
    //
    //         // console.log(JSON.stringify(ret));
    //         var ids = ret.data.id;
    //         api.openWin({
    //             name: 'shop_order_details',
    //             url: './shop_order_details.html',
    //             pageParam: {
    //                 ids: ids
    //             }
    //         });
    //
    //
    //       } else {
              // alert(JSON.stringify(err));
    //       }
    //   });
    //
    //
    // }
</script>

</html>
